// src/utils/request.js
import axios from 'axios'
import { Message } from 'element-ui'


// 创建axios实例
const service = axios.create({
    baseURL: '',
    timeout: 5000
})

// 请求拦截器
service.interceptors.request.use(
    config => {
        // 登录请求不需要携带token
        if (config.url === '/login' || config.url === '/login/sms' || config.url === '/login/email'||config.url === '/register') {
            return config
        }
    
        const token = localStorage.getItem('authentication')
      
        if (token) {
            config.headers['authentication'] = token
        }
        return config
    },
    error => {
        console.log(error)
        return Promise.reject(error)
    }
)

// 响应拦截器
service.interceptors.response.use(
    response => {
        return response
    },
    error => {
        console.log('err' + error)
        Message.error(error.message || '请求失败')
        return Promise.reject(error)
    }
)

export default service